<template>
  <div>
    <div style="height:20px;"></div>
    <FormPanel ref="from" :list="formList" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <FilterTable
      ref="table"
      :columns="columns"
      :dataSource="tableList"
      :isSelectColumn="false"
      height='auto'
      :onColumnsChange="columns => this.columns = columns"
    >
       <template slot="controls" slot-scope="props">
        <multiuse-button  size="small" type="primary" @click="addHandle(1)" >新 增</multiuse-button>
      </template>
    </FilterTable>
     <FilterTable
      ref="table1"
      :columns="columns1"
      :dataSource="tableList1"
      :isSelectColumn="false"
      height='auto'
      :onColumnsChange="columns1 => this.columns1 = columns1"
    >

    </FilterTable>
    <BaseDialog
      :visible.sync="issearch"
      :title="'基本信息'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <sapEmit  :data="formData" :dealerCode="dealerCode" @close="changeData" />
    </BaseDialog>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">返 回</el-button>
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)">保 存</multiuse-button>
    </div>
  </div>
</template>
<script>
import {getDetail,postSave,getSap} from '@/api/cms-api/origin/sapDetail/index.js';
import { dictionary } from '@/utils/dictMixin';
import sapEmit from './sapEmit';
import moment from 'moment'
import Cookie from 'js-cookie'
import {isArcfox } from '@/api/cms-api/origin/commonDealerType';
export default {
  data(){
    this.saveData=null;
    this.table=null;
    this.table1=null;
    return{
      formList:this.createdFrom(),
      formData:{},
      dealerCode:'',
      issearch:false,
      tableList:[],
      tableList1:[],
      columns:this.createdTable(),
      columns1:this.createdTable1(),
      orgList:[

      ],
      deptList:[
        {text:'南方销售办公室',value:'0001'},
        {text:'大客户部',value:'S000'},
        {text:'华北事业部',value:'S001'},
        {text:'华东事业部',value:'S002'},
        {text:'华中事业部',value:'S003'},
        {text:'华南事业部',value:'S004'},
        {text:'西南事业部',value:'S005'},
        {text:'西北事业部',value:'S006'},
      ],

      userList:[
        {text:'专票',value:0},
        {text:'机动车发票',value:2},
        {text:'普票',value:12},
      ],
      priceList:[
        {text:'立即应付的到期净值',value:'0001'},
        {text:'14天之内2%的现金折扣，30天之内到期净值',value:'0002'},
        {text:'14天之内的3%的现金折扣，20天之内2%的现金折扣，30天之内到期净值',value:'0003'},
        {text:'立即应付的到期净值，基准日期为当月结束',value:'0004'},
        {text:'立即应付的到期净值，基准日期为下月10',value:'0005'}
      ],
      accountList:[
        {text:'国内客户',value:'01'},
        {text:'国外客户',value:'02'}
      ],
      taxList:[{
        text:'免税',value:'0'
      },{
        text:'必须上税',value:'1'
      }]
    }
  },
  mixins:[dictionary],
  components:{
    sapEmit,
  },
  mounted(){
    this.table=this.$refs.table.$refs.pageTable;

  },
  props:['data'],
  created(){
    if(isArcfox()){
      this.orgList=[
        {text:'北京蓝谷极狐投资管理有限公司销售组织',value:1012},
        {text:'北京蓝谷极狐投资管理有限公司配件销售组织',value:'112A'},
        ]
    }else{
      this.orgList=[{text:'北京新能源汽车股份有限公司青岛分公司',value:1051},
        {text:'北京新能源汽车常州有限公司',value:1052},
        {text:'北京新能源汽车营销有限公司',value:2000},
        {text:'青岛卫蓝销售公司',value:2001},
        {text:'北京新能源汽车营销有限公司配件销售组织',value:'200A'}]
    }
    this.getDetail(this.data)
    this.getSap(this.data)

  },
  methods:{
    createdFrom(){
      return [
        {
          type: 'INPUT',
          label: '新能源SAP代码',
          fieldName: 'sapCode',
          placeholder: '请选择',
          disabled:false,
          rules: [{ required: true, message: '请输入', trigger: 'blur' }],
        },


      ]
    },
    createdTable1(){
       return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
         {
          title: '传输状态',
          dataIndex: 'status',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
         {
          title: '错误日志消息',
          dataIndex: 'errMsg',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
         {
          title: '创建时间',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'input',
        }
        ]

    },
    createdTable(){
      let userList=[
        {text:'专票',value:'0'},
        {text:'机动车发票',value:'2'},
        {text:'普票',value:'12'},
      ],
      priceList=[
        {text:'立即应付的到期净值',value:'0001'},
        {text:'14天之内2%的现金折扣，30天之内到期净值',value:'0002'},
        {text:'14天之内的3%的现金折扣，20天之内2%的现金折扣，30天之内到期净值',value:'0003'},
        {text:'立即应付的到期净值，基准日期为当月结束',value:'0004'},
        {text:'立即应付的到期净值，基准日期为下月10',value:'0005'}
      ],
      accountList=[
        {text:'国内客户',value:'01'},
        {text:'国外客户',value:'02'}
      ],
      taxList=[{
        text:'免税',value:'0'
      },{
        text:'必须上税',value:'1'
      }],
      Trade=[{
        text:'成本和运费',value:'CFR'
      },{
        text:'成本,保险&运费',value:'CIF'
      },
      {
        text:'运费和保险费已付',value:'CIP'
      },{
        text:'运费已付',value:'CPT'
      },{
        text:'边境交货',value:'DAF'
      },{
        text:'完税后交货价',value:'DDP'
      },{
        text:'未完税交货价',value:'DDU'
      },{
        text:'码头交货(完税)',value:'DEQ'
      },{
        text:'船上交货',value:'DES'
      },{
        text:'工厂交货',value:'EXW'
      },{
        text:'免靠船',value:'FAS'
      },{
        text:'免运费',value:'FCA'
      },{
        text:'仓库交货',value:'FH'
      },{
        text:'离岸价',value:'FOB'
      },{
        text:'不免费',value:'UN'
      }];

      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 200,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button  size="mini" type="text" onClick={() => this.editHandle(props.row,1)} >
                  编辑
                </multiuse-button>
                <el-divider direction="vertical" />
                <multiuse-button  size="mini" type="text" onClick={() => this.editHandle(props.row,2)} >
                  删除
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '销售组织名称',
          dataIndex: 'salesOrgName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '销售部门',
          dataIndex: 'salesDeptName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '销售组',
          dataIndex: 'salesTeamName',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: userList,
          editItems:userList,
        },
        {
          title: '国际贸易术语1',
          dataIndex: 'internationalTradeTermsFirst',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: Trade,
          editItems:Trade,
        },
        {
          title: '国际贸易术语2',
          dataIndex: 'internationalTradeTermsSecond',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: Trade,
          editItems:Trade,
        },
        {
          title: '付款条件',
          dataIndex: 'paymentTerms',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: priceList,
          editItems:priceList,
        },
        {
          title: '账户分配组',
          dataIndex: 'accountAllocationGroup',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: accountList,
          editItems:accountList,
        },
        {
          title: '税分类',
          dataIndex: 'taxClassification',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: taxList,
          editItems:taxList,
        },
        {
          title: '收票方',
          dataIndex: 'beneficiary',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '付款方',
          dataIndex: 'payer',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '送方达',
          dataIndex: 'deliveryParty',
          sorter: true,
          filter: true,
          filterType: 'input',
        },


      ]
    },
    addHandle(){
      this.formData=null;
      this.issearch=true;
    },

    changeHandle(val){
      this.saveData=val;
    },
    isRepeat(arr) {
        var hash = {};
        for (var i in arr) {
            if (hash[arr[i]]){
                return true;
            }
            hash[arr[i]] = true;
        }
        return false;
    },
    async closeDrawer(type){
       if (type == 1) {
        this.$emit('close');
      } else if (type == 2) {
        this.$refs.from.SUBMIT_FORM();
        if (this.saveData) {
          let json = JSON.parse(JSON.stringify(this.saveData));
          if(this.tableList.length<1){
            this.$notify({
              title: '警告',
              message: '请维护表格信息',
              type: 'warning'
            });
            return;
          }
          json.list=this.tableList;
          json.dealerCode=this.data.dealerCode
          let arr=[]
          json.list.map(x=>{
            x.dealerAttachId=this.data.dealerAttachId;
            arr.push(x.salesOrgId);
          })
          if(this.isRepeat(arr)){
            this.$notify({
              title: '警告',
              message: '组织不允许重复，请重新维护',
              type: 'warning'
            });
            return;
          }
          json.dealerAttachId=this.data.dealerAttachId
          let res = await postSave(json);
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '保存成功',
              type: 'success'
            });
            this.$emit('close', json);
          } else {
            this.saveData = null;
          }
        }
      }
    },
    async getDetail(data){
      let res=await getDetail({dealerAttachId:data.dealerAttachId});
      this.dealerCode=data.dealerCode;
      this.tableList = res.data||[];
      if(this.data.sapCode){
        this.formList.find(x=>x.fieldName=='sapCode').disabled=true;
        this.formList.find(x=>x.fieldName=='sapCode').initialValue=this.data.sapCode;
      }else{
        this.formList.find(x=>x.fieldName=='sapCode').disabled=false;
      }


    },
    async getSap(data){
      let res=await getSap({sapCode:data.sapCode});

      this.tableList1 = res.data||[];


    },
    async editHandle(data,type){
      if(type==2){
        await this.$confirm('是否确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        this.table.EXECUTE_DELETE([data]);
      })
      .catch(()=>{})
      }else{
        this.formData=data;
        this.issearch=true;
      }
    },
    changeData(val){
      if(val){
        let is=false;
        this.tableList.map(x=>{
          if(x._uid==val._uid){
            is=true
            x.salesOrgName=val.salesOrgName;
            x.salesOrgId=val.salesOrgId;
            x.salesDeptName=val.salesDeptName;
            x.salesDeptId=val.salesDeptId;
            x.salesTeamName=val.salesTeamName;
            x.customerTeamId=val.customerTeamId;
            x.internationalTradeTermsFirst=val.internationalTradeTermsFirst;
            x.internationalTradeTermsSecond=val.internationalTradeTermsSecond;
            x.paymentTerms=val.paymentTerms;
            x.accountAllocationGroup=val.accountAllocationGroup;
            x.taxClassification=val.taxClassification;
            x.beneficiary=val.beneficiary;
            x.payer=val.payer;
            x.deliveryParty=val.deliveryParty;
          }
        })
        console.log(is,'is')
        if(!is){
          this.table.EXECUTE_INSERT([val])
        }
      }
      this.issearch=false;
    }
  }
}
</script>
